|
1
|
|
|
export default renderCanvas; |
|
2
|
|
|
|
|
3
|
|
|
import merge from "../help/merge.js"; |
|
4
|
|
|
import {getEncodingHeight, getBarcodePadding} from "./shared.js" |
|
5
|
|
|
|
|
6
|
|
|
function renderCanvas(canvas, encodings, options){ |
|
7
|
|
|
// Abort if the browser does not support HTML5 canvas |
|
8
|
|
|
if (!canvas.getContext) { |
|
9
|
|
|
throw new Error('The browser does not support canvas.'); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
prepareCanvas(canvas, options, encodings); |
|
13
|
|
|
for(let i = 0; i < encodings.length; i++){ |
|
14
|
|
|
var encodingOptions = merge(options, encodings[i].options); |
|
15
|
|
|
|
|
16
|
|
|
drawCanvasBarcode(canvas, encodingOptions, encodings[i]); |
|
17
|
|
|
drawCanvasText(canvas, encodingOptions, encodings[i]); |
|
18
|
|
|
|
|
19
|
|
|
moveCanvasDrawing(canvas, encodings[i]); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
restoreCanvas(canvas); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
function prepareCanvas(canvas, globalOptions, encodings){ |
|
26
|
|
|
// Get the canvas context |
|
27
|
|
|
var ctx = canvas.getContext("2d"); |
|
28
|
|
|
|
|
29
|
|
|
ctx.save(); |
|
30
|
|
|
|
|
31
|
|
|
// Calculate total width |
|
32
|
|
|
var totalWidth = 0; |
|
33
|
|
|
var maxHeight = 0; |
|
34
|
|
|
for(let i = 0; i < encodings.length; i++){ |
|
35
|
|
|
var options = merge(globalOptions, encodings[i].options); |
|
36
|
|
|
|
|
37
|
|
|
// Set font |
|
38
|
|
|
ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font; |
|
39
|
|
|
|
|
40
|
|
|
// Calculate the width of the encoding |
|
41
|
|
|
var textWidth = ctx.measureText(encodings[i].text).width; |
|
42
|
|
|
var barcodeWidth = encodings[i].data.length * options.width; |
|
43
|
|
|
encodings[i].width = Math.ceil(Math.max(textWidth, barcodeWidth)); |
|
44
|
|
|
|
|
45
|
|
|
// Calculate the height of the encoding |
|
46
|
|
|
var height = getEncodingHeight(encodings[i], options); |
|
47
|
|
|
|
|
48
|
|
|
encodings[i].barcodePadding = getBarcodePadding(textWidth, barcodeWidth, options); |
|
49
|
|
|
|
|
50
|
|
|
if(height > maxHeight){ |
|
51
|
|
|
maxHeight = height; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
totalWidth += encodings[i].width; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
canvas.width = totalWidth + globalOptions.marginLeft + globalOptions.marginRight; |
|
58
|
|
|
|
|
59
|
|
|
canvas.height = maxHeight; |
|
60
|
|
|
|
|
61
|
|
|
// Paint the canvas |
|
62
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height); |
|
63
|
|
|
if(globalOptions.background){ |
|
64
|
|
|
ctx.fillStyle = globalOptions.background; |
|
65
|
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
ctx.translate(globalOptions.marginLeft, 0); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function drawCanvasBarcode(canvas, options, encoding){ |
|
72
|
|
|
// Get the canvas context |
|
73
|
|
|
var ctx = canvas.getContext("2d"); |
|
74
|
|
|
|
|
75
|
|
|
var binary = encoding.data; |
|
76
|
|
|
|
|
77
|
|
|
// Creates the barcode out of the encoded binary |
|
78
|
|
|
var yFrom; |
|
79
|
|
|
if(options.textPosition == "top"){ |
|
80
|
|
|
yFrom = options.marginTop + options.fontSize + options.textMargin; |
|
81
|
|
|
} |
|
82
|
|
|
else{ |
|
83
|
|
|
yFrom = options.marginTop; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
ctx.fillStyle = options.lineColor; |
|
87
|
|
|
|
|
88
|
|
|
for(var b = 0; b < binary.length; b++){ |
|
89
|
|
|
var x = b * options.width + encoding.barcodePadding; |
|
90
|
|
|
|
|
91
|
|
|
if(binary[b] === "1"){ |
|
92
|
|
|
ctx.fillRect(x, yFrom, options.width, options.height); |
|
93
|
|
|
} |
|
94
|
|
|
else if(binary[b]){ |
|
95
|
|
|
ctx.fillRect(x, yFrom, options.width, options.height * binary[b]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function drawCanvasText(canvas, options, encoding){ |
|
101
|
|
|
// Get the canvas context |
|
102
|
|
|
var ctx = canvas.getContext("2d"); |
|
103
|
|
|
|
|
104
|
|
|
var font = options.fontOptions + " " + options.fontSize + "px " + options.font; |
|
105
|
|
|
|
|
106
|
|
|
// Draw the text if displayValue is set |
|
107
|
|
|
if(options.displayValue){ |
|
108
|
|
|
var x, y; |
|
109
|
|
|
|
|
110
|
|
|
if(options.textPosition == "top"){ |
|
111
|
|
|
y = options.marginTop + options.fontSize - options.textMargin; |
|
112
|
|
|
} |
|
113
|
|
|
else{ |
|
114
|
|
|
y = options.height + options.textMargin + options.marginTop + options.fontSize; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
ctx.font = font; |
|
118
|
|
|
|
|
119
|
|
|
// Draw the text in the correct X depending on the textAlign option |
|
120
|
|
|
if(options.textAlign == "left" || encoding.barcodePadding > 0){ |
|
121
|
|
|
x = 0; |
|
122
|
|
|
ctx.textAlign = 'left'; |
|
123
|
|
|
} |
|
124
|
|
|
else if(options.textAlign == "right"){ |
|
125
|
|
|
x = encoding.width - 1; |
|
126
|
|
|
ctx.textAlign = 'right'; |
|
127
|
|
|
} |
|
128
|
|
|
// In all other cases, center the text |
|
129
|
|
|
else{ |
|
130
|
|
|
x = encoding.width / 2; |
|
131
|
|
|
ctx.textAlign = 'center'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
ctx.fillText(encoding.text, x, y); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
function moveCanvasDrawing(canvas, encoding){ |
|
141
|
|
|
var ctx = canvas.getContext("2d"); |
|
142
|
|
|
|
|
143
|
|
|
ctx.translate(encoding.width, 0); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
function restoreCanvas(canvas){ |
|
147
|
|
|
// Get the canvas context |
|
148
|
|
|
var ctx = canvas.getContext("2d"); |
|
149
|
|
|
|
|
150
|
|
|
ctx.restore(); |
|
151
|
|
|
} |
|
152
|
|
|
|